#代码如下：
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b95a154-79e8-4c47-9e8a-660f657c5c5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "个人账单管理系统\n",
      "1. 记录收入\n",
      "2. 记录支出\n",
      "3. 查询收入记录\n",
      "4. 查询支出记录\n",
      "5. 生成统计报告\n",
      "6. 退出\n"
     ]
    }
   ],
   "source": [
    "import datetime  \n",
    "  #足够\n",
    "# 数据存储  \n",
    "income_records = []  \n",
    "expense_records = []  \n",
    "  \n",
    "def record_income():  \n",
    "    date = input(\"请输入日期 (YYYY-MM-DD): \")  \n",
    "    amount = float(input(\"请输入收入金额: \"))  \n",
    "    if amount <= 0:  \n",
    "        print(\"收入金额必须为正数！\")  \n",
    "        return  \n",
    "    category = input(\"请输入收入类别: \")  \n",
    "    remark = input(\"请输入备注 (可选): \")  \n",
    "      \n",
    "    record = {  \n",
    "        \"date\": date,  \n",
    "        \"amount\": amount,  \n",
    "        \"category\": category,  \n",
    "        \"remark\": remark  \n",
    "    }  \n",
    "    income_records.append(record)  \n",
    "    print(\"收入记录成功！\")  \n",
    "  \n",
    "def record_expense():  \n",
    "    date = input(\"请输入日期 (YYYY-MM-DD): \")  \n",
    "    amount = float(input(\"请输入支出金额: \"))  \n",
    "    if amount <= 0:  \n",
    "        print(\"支出金额必须为正数！\")  \n",
    "        return  \n",
    "    category = input(\"请输入支出类别: \")  \n",
    "    remark = input(\"请输入备注 (可选): \")  \n",
    "      \n",
    "    record = {  \n",
    "        \"date\": date,  \n",
    "        \"amount\": amount,  \n",
    "        \"category\": category,  \n",
    "        \"remark\": remark  \n",
    "    }  \n",
    "    expense_records.append(record)  \n",
    "    print(\"支出记录成功！\")  \n",
    "  \n",
    "def query_records(records, record_type):  \n",
    "    print(f\"查询 {record_type.capitalize()} 记录\")  \n",
    "    query_type = input(\"请输入查询类型 (日期范围/类别/备注): \").strip().lower()  \n",
    "      \n",
    "    if query_type == \"日期范围\":  \n",
    "        start_date = input(\"请输入开始日期 (YYYY-MM-DD): \")  \n",
    "        end_date = input(\"请输入结束日期 (YYYY-MM-DD): \")  \n",
    "          \n",
    "        matched_records = [record for record in records if start_date <= record[\"date\"] <= end_date]  \n",
    "    elif query_type == \"类别\":  \n",
    "        category = input(\"请输入类别: \")  \n",
    "        matched_records = [record for record in records if record[\"category\"] == category]  \n",
    "    elif query_type == \"备注\":  \n",
    "        remark = input(\"请输入备注: \")  \n",
    "        matched_records = [record for record in records if remark in record[\"remark\"]]  \n",
    "    else:  \n",
    "        print(\"无效的查询类型！\")  \n",
    "        return  \n",
    "      \n",
    "    for record in matched_records:  \n",
    "        print(record)  \n",
    "  \n",
    "def generate_statistics():  \n",
    "    total_income = sum(record[\"amount\"] for record in income_records)  \n",
    "    total_expense = sum(record[\"amount\"] for record in expense_records)  \n",
    "      \n",
    "    income_categories = {}  \n",
    "    expense_categories = {}  \n",
    "      \n",
    "    for record in income_records:  \n",
    "        if record[\"category\"] in income_categories:  \n",
    "            income_categories[record[\"category\"]] += record[\"amount\"]  \n",
    "        else:  \n",
    "            income_categories[record[\"category\"]] = record[\"amount\"]  \n",
    "      \n",
    "    for record in expense_records:  \n",
    "        if record[\"category\"] in expense_categories:  \n",
    "            expense_categories[record[\"category\"]] += record[\"amount\"]  \n",
    "        else:  \n",
    "            expense_categories[record[\"category\"]] = record[\"amount\"]  \n",
    "      \n",
    "    print(f\"\\n总收入: {total_income}\")  \n",
    "    print(f\"总支出: {total_expense}\")  \n",
    "    print(\"\\n收入分类:\")  \n",
    "    for category, amount in income_categories.items():  \n",
    "        print(f\"{category}: {amount}\")  \n",
    "    print(\"\\n支出分类:\")  \n",
    "    for category, amount in expense_categories.items():  \n",
    "        print(f\"{category}: {amount}\")  \n",
    "  \n",
    "def main():  \n",
    "    while True:  \n",
    "        print(\"\\n个人账单管理系统\")  \n",
    "        print(\"1. 记录收入\")  \n",
    "        print(\"2. 记录支出\")  \n",
    "        print(\"3. 查询收入记录\")  \n",
    "        print(\"4. 查询支出记录\")  \n",
    "        print(\"5. 生成统计报告\")  \n",
    "        print(\"6. 退出\")  \n",
    "          \n",
    "        choice = input(\"请选择操作: \")  \n",
    "          \n",
    "        if choice == \"1\":  \n",
    "            record_income()  \n",
    "        elif choice == \"2\":  \n",
    "            record_expense()  \n",
    "        elif choice == \"3\":  \n",
    "            query_records(income_records, \"收入\")  \n",
    "        elif choice == \"4\":  \n",
    "            query_records(expense_records, \"支出\")  \n",
    "        elif choice == \"5\":  \n",
    "            generate_statistics()  \n",
    "        elif choice == \"6\":  \n",
    "            print(\"退出系统。\")  \n",
    "            break  \n",
    "        else:  \n",
    "            print(\"无效的选择，请重新输入！\")  \n",
    "  \n",
    "if __name__ == \"__main__\":  \n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab252a9b-fae6-4bbd-bcc7-61687318d9b7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
